package com.hxh.wechat.activity

import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.viewpager.widget.ViewPager
import androidx.viewpager.widget.ViewPager.SimpleOnPageChangeListener
import com.hxh.wechat.R
import com.hxh.wechat.adpter.ImageViewPagerAdapter


class PhotoViewActivity : BaseActivity() {
    private var mImageViewPager: ViewPager? = null
    private var mCurImageNum: TextView? = null
    private var mCurrentPosition = 0
    private var mImageList: Array<String>? = null
    private var imageUrl: String? = null
    protected fun initLayout(savedInstanceState: Bundle?) {
        setContentView(R.layout.activity_preview)
    }

    protected fun initViews() {
        val intent = getIntent()
        mCurrentPosition = intent.getIntExtra("currentPosition", 0)
        mImageList = intent.getSerializableExtra("imageUrlList") as Array<String>?
        mImageViewPager = findViewById(R.id.view_pager)
        mCurImageNum = findViewById(R.id.image_num)
    }

    protected fun initEvents() {
        val adapter: ImageViewPagerAdapter<*> = ImageViewPagerAdapter(mImageList!!.toList(), this)
        mImageViewPager!!.adapter = adapter
        mImageViewPager!!.setCurrentItem(mCurrentPosition, false)
        mCurImageNum!!.text = (mCurrentPosition + 1).toString() + "/" + mImageList!!.size
        mImageViewPager!!.addOnPageChangeListener(object : SimpleOnPageChangeListener() {
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                mCurrentPosition = position
                mCurImageNum!!.text = (mCurrentPosition + 1).toString() + "/" + mImageList!!.size
            }
        })
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initLayout(savedInstanceState)
        initViews()
        initEvents()
        findViewById<Button>(R.id.preview_back).setOnClickListener {
            finish()
        }
    }
}